package com.example.administrator.funclient;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.funclient.adapter.MessageAdapter;
import com.example.administrator.funclient.model.MsgBean;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView clientRv;
    private EditText clientEt;
    private Button clientBt;
    private String serverIP = "132.232.152.212";
    private int serverPort = 11111;
    private SocketThread socketThread;
    private Socket socket = null;
    private BufferedWriter bw = null;
    private String name = "a";
    private Handler handler;
    private String uuid=  UUID.randomUUID().toString();
    private List<MsgBean> mblist=new ArrayList<>();
    MessageAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        clientRv = findViewById(R.id.clientRv);
        clientEt = findViewById(R.id.clientEt);
        clientBt = findViewById(R.id.clientBt);
         adapter=new MessageAdapter(mblist,uuid);
        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
        manager.setStackFromEnd(true);
        clientRv.setLayoutManager(manager);
        clientRv.setAdapter(adapter);

        socketThread = new SocketThread();
        socketThread.start();
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if (message.what == 1) {
                    mblist.add((MsgBean) message.obj);
                    adapter.notifyDataSetChanged();
                }
                return false;
            }
        });
        clientBt.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        final String clientMsg = clientEt.getText().toString();
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    OutputStream os = socket.getOutputStream();
                    bw = new BufferedWriter(new OutputStreamWriter(os, "utf-8"));
                    SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
                    MsgBean mb = new MsgBean();
                    mb.setName(name);
                    mb.setDate(dateFormat.format(Calendar.getInstance().getTime()));
                    mb.setClientMsg(clientMsg);
                    mb.setUuID(uuid);
                    bw.write(mb.toString(), 0, mb.toString().length());
                    bw.flush();
                    Log.e("send==>", mb.toString() + "\n");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        clientEt.getText().clear();
    }

    class SocketThread extends Thread {
        @Override
        public void run() {
            try {
                socket = new Socket(InetAddress.getByName(serverIP), serverPort);
                InputStream is = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
                String line = null;
                Log.e("====","begin");
                while ((line = br.readLine()) != null) {
                    Log.i("=======", line);
                    MsgBean mb = new MsgBean();
                    mb = mb.spiltBean(line);
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = mb;
                    handler.sendMessage(msg);
                    Log.i("=======", mb.getName() + mb.getClientMsg() + "-----");
                }

            } catch (IOException e) {
                Log.e("====",""+ e.getMessage());
            }
        }
    }
}
